﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;
using MINERP.BLL.InfoConfig;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using MINERP.ComControls.Extension;

namespace MINERP.InfoConfig
{
    public partial class FrmShopManager : Form
    {
        Shop_New_BLL _bll;
        private FrmShopManager()
        {
            InitializeComponent();
            _bll = new Shop_New_BLL();
            gridViewShop.ShowRowIndicator(50);
        }
        private static FrmShopManager _instance;

        public static FrmShopManager CreateInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmShopManager();
                }
                return _instance;
            }
        }

        int rowhandle = 0;
        int intShopBaseOpeType = 0;//0新增1修改-1原始

        List<PaperType_Info> AllPaperType = new List<PaperType_Info>();
        List<PaperFace_Info> AllPaperFace = new List<PaperFace_Info>();
        Shop_New_Info CurrentShopInfo = new Shop_New_Info();///更新时固定选择项
        List<Shop_Paper_Price_Info> currShopPaperPriceInfo = new List<Shop_Paper_Price_Info>();

        List<tb_CostType_info> AllCostType = new List<tb_CostType_info>();

        List<Shop_CommonChargeItem_Price_Info> ListShopCommonChangeItemPriceInfo = new List<Shop_CommonChargeItem_Price_Info>();
        List<ProductDDO> ListProduct = new List<ProductDDO>();

        shop_DistributionInfo_Info shopDistributionInfo = new shop_DistributionInfo_Info();



        private void gridViewShop_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void FrmShopManager_New_Load(object sender, EventArgs e)
        {
            initForm();

        }


        public void initForm()
        {
            AllPaperType = _bll.GetAllPaperType();
            AllPaperFace = _bll.GetAllPaperFace();
            List<Shop_New_Info> listShop = _bll.GetAllShopList();
            gridControlShop.DataSource = listShop;

            cmbPaperType.DataSource = AllPaperType;
            cmbPaperType.DisplayMember = "Name";
            cmbPaperType.ValueMember = "PaperTypeID";

            cmbPaperFace.DataSource = AllPaperFace;
            cmbPaperFace.ValueMember = "PaperFaceID";
            cmbPaperFace.DisplayMember = "Name";

            btnAddNewShop.Enabled = true;
            simpleButtonSave.Enabled = false;
            btnUpdateShop.Enabled = true;
            btnDeleteShop.Enabled = true;


            //收费类型
            // gridControlShopPaperTypePrice.Enabled = false;
            gridColumnUnitPrice.OptionsColumn.AllowEdit = false;

            gcShopPaperFlag.OptionsColumn.AllowEdit = false;
           // gcShopPaperFlag.OptionsColumn.AllowEdit = false;
            simpleButtonSPTPok.Enabled = false;


            //收费类型绑定
            AllCostType = _bll.GetAllCostType();
            foreach (tb_CostType_info type in AllCostType)
            {
                repositoryItemComboBoxCostType.Items.Add(type.TypeName);
                repositoryItemComboBoxProductCostTpye.Items.Add(type.TypeName);
            }


            btnSccipSave.Enabled = false;
            // gridControlShopCommonCostType.Enabled = false;

            {//商户公共收费项目
                gridColumnItemName.OptionsColumn.AllowEdit = false;
                gridColumnSccSinglePrice.OptionsColumn.AllowEdit = false;
                gridColumnSCCTAreaPrice.OptionsColumn.AllowEdit = false;
                gridColumnSCCUnitPrice.OptionsColumn.AllowEdit = false;
                gridColumnSCCTypeName.OptionsColumn.AllowEdit = false;
                GCCommFlag.OptionsColumn.AllowEdit = false;
               
            }

            //gridControlProduct.Enabled = false;
            {
                IsChecked.OptionsColumn.AllowEdit = false;
                gridColumn23.OptionsColumn.AllowEdit = false;
                gridColumn2.OptionsColumn.AllowEdit = false;
                gridColumn3.OptionsColumn.AllowEdit = false;
                gridColumn4.OptionsColumn.AllowEdit = false;
            }
            btnShopProductPriceOK.Enabled = false;


            groupBoxShopDistrion.Enabled = false;
            simpleButton2.Enabled = false;
            simpleButtonSelectAll.Enabled = false;
            simpleButtonSelectOther.Enabled = false;



        }

        private void gridViewShop_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                Shop_New_Info curShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.RowHandle);
                if (curShopInfo.Status == 0)//表示被删除
                {
                    e.Appearance.BackColor = Color.Red;
                }
            }
        }

        private void gridViewShop_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //rowhandle = e.FocusedRowHandle;
            //焦点改变时在详细里显示相应的信息
            if (e.FocusedRowHandle > -1)
            {
                //if (Tabcontrol1.SelectedTabPageIndex == 0)//详细信息
                //{
                //    #region//基础信息
                //    if (intShopBaseOpeType != 1)//不是更新可变
                //    {
                //        Shop_New_Info curShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.FocusedRowHandle);
                //        txtAddress.Text = curShopInfo.Addr;
                //        TxtArea.Text = curShopInfo.AreaName;
                //        txtCreditLevel.Text = curShopInfo.CreditLevel.ToString();
                //        txtEmail.Text = curShopInfo.Email;
                //        txtMnemonicCode.Text = curShopInfo.MiniCode;
                //        txtMobile.Text = curShopInfo.MobilePhoneNum;
                //        txtMSN.Text = curShopInfo.MSN;
                //        txtQQ.Text = curShopInfo.QQ;
                //        txtRemark.Text = curShopInfo.Remark;
                //        txtSalesman.Text = curShopInfo.SalesMan;
                //        txtShopName.Text = curShopInfo.ShopName.Substring(1);
                //        txtTelePhone.Text = curShopInfo.PhoneNum;
                //        cmbPaperFace.Text = AllPaperFace.FirstOrDefault(p => p.PaperFaceID == curShopInfo.PaperFaceID) != null ?
                //            AllPaperFace.FirstOrDefault(p => p.PaperFaceID == curShopInfo.PaperFaceID).Name : "";
                //        cmbPaperType.Text = AllPaperType.FirstOrDefault(P => P.PaperTypeID == curShopInfo.PaperTypeID) != null ?
                //            AllPaperType.FirstOrDefault(P => P.PaperTypeID == curShopInfo.PaperTypeID).Name : "";
                //        cmbPriority.Text = curShopInfo.Priority.ToString();
                //        chkIsNeedPrintUPrice.Checked = curShopInfo.IsPrintPrice;
                //        txtkeeper.Text = curShopInfo.ContactPerson;
                //        checkEditIsAssignByCustom.Checked = curShopInfo.IsAssignByCustom;
                //    }
                //    #endregion

                //}
                //else if (Tabcontrol1.SelectedTabPageIndex == 1)//计价收费
                //{
                //    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();
                //    currShopPaperPriceInfo = _bll.GetPaperPriceByShopId(CurrentShopInfo.ShopID);
                //    gridControlShopPaperTypePrice.DataSource = currShopPaperPriceInfo;

                //    ListShopCommonChangeItemPriceInfo =
                //        _bll.GetShopCommonChangeItemPriceInfoByShopId(CurrentShopInfo.ShopID);

                //    gridControlShopCommonCostType.DataSource = ListShopCommonChangeItemPriceInfo;

                //}
                //else if (Tabcontrol1.SelectedTabPageIndex == 2)//产品列表
                //{

                //    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();

                //    ListProduct = _bll.GetAllShopProducts(CurrentShopInfo.ShopID);
                //    gridControlProduct.DataSource = ListProduct;

                //}
                //else if (Tabcontrol1.SelectedTabPageIndex == 3)//货运信息
                //{
                //    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();
                //    shopDistributionInfo = _bll.GetShopDistributionInfo(CurrentShopInfo.ShopID);
                //    DisPlayDisribution(shopDistributionInfo);
                //}
                //else//远程FTP
                //{
                //    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();

                //}
                
            }
        }


        //添加新商户
        private void btnAddNewShop_Click(object sender, EventArgs e)
        {
            //添加商户

            groupBoxShopBaseInfo.Enabled = true;
            txtAddress.Text = "";
            TxtArea.Text = "";
            txtCreditLevel.Text = "";
            txtEmail.Text = "";
            txtMnemonicCode.Text = "";
            txtMobile.Text = "";
            txtMSN.Text = "";
            txtQQ.Text = "";
            txtRemark.Text = "";
            txtSalesman.Text = "";
            txtShopName.Text = "";
            txtTelePhone.Text = "";
            cmbPaperFace.Text = "";
            cmbPaperType.Text = "";
            cmbPriority.Text = "";
            intShopBaseOpeType = 0;//新增加
            btnAddNewShop.Enabled = false;
            simpleButtonSave.Enabled = true;
            btnUpdateShop.Enabled = false;
            btnDeleteShop.Enabled = false;
            checkEditIsAssignByCustom.Checked = false;
        }

        #region help methods
        //给商户列表符值
        private void IniltShopGridView()
        {
            if (checkBoxDisplayDelShop.CheckState == CheckState.Checked)
            {
                this.gridControlShop.DataSource = _bll.GetAllShopList();
            }
            else
            {
                this.gridControlShop.DataSource = _bll.GetAllShopList().Where(st=>st.Status==1);
            }
        }

        #endregion

        private void txtShopName_Leave(object sender, EventArgs e)
        {

            string shopNameCode = GetMcode(txtShopName.Text);
            txtMnemonicCode.Text = shopNameCode;
            if (_bll.CheckShopName(txtShopName.Text))
            {
                ShowAlarmMsg("商户名称已存在");
            }

        }

        private string GetMcode(string CString)
        {
            StringBuilder temp = new StringBuilder();
            char[] charTem = CString.ToCharArray();
            foreach (char ch in charTem)
            {
                if (Convert.ToInt32(ch) < Convert.ToInt32(Convert.ToChar(128)))
                {
                    temp.Append(ch);
                }
                else
                {
                    temp.Append(MnemonicCode.chinesecap(ch.ToString()));
                }
            }
            return temp.ToString().ToUpper();
        }

        private void simpleButtonSave_Click(object sender, EventArgs e)
        {
            if (intShopBaseOpeType == 0)
            {
                #region//添加新商户
                if (txtShopName.Text.Trim() == "")
                {
                    ShowAlarmMsg("商户名称不能为空！");
                    return;
                }
                if (txtMnemonicCode.Text.Trim() == "")
                {
                    txtMnemonicCode.Text = GetMcode(txtShopName.Text.Trim());

                }
                Shop_New_Info shopNew = new Shop_New_Info();
                shopNew.Addr = txtAddress.Text.Trim() != "" ? txtAddress.Text.Trim() : "0";
                shopNew.AreaName = TxtArea.Text.Trim() != "" ? TxtArea.Text.Trim() : "0";
                shopNew.CreditLevel = Convert.ToInt32(txtCreditLevel.Text.Trim() == "" ? "0" : txtCreditLevel.Text.Trim());
                shopNew.Email = txtEmail.Text.Trim() != "" ? txtEmail.Text.Trim() : "0";
                shopNew.MiniCode = txtMnemonicCode.Text.Trim() != "" ? txtMnemonicCode.Text.Trim() : "0";
                shopNew.MobilePhoneNum = txtMobile.Text.Trim() != "" ? txtMobile.Text.Trim() : "0";
                shopNew.MSN = txtMSN.Text.Trim() != "" ? txtMSN.Text.Trim() : "0";
                shopNew.QQ = txtQQ.Text.Trim() != "" ? txtQQ.Text.Trim() : "0";
                shopNew.Remark = txtRemark.Text.Trim() != "" ? txtRemark.Text.Trim() : "0";
                shopNew.SalesMan = txtSalesman.Text.Trim() != "" ? txtSalesman.Text.Trim() : "0";

                if (shopNew.MiniCode.Length > 1)
                {
                    String startChar = shopNew.MiniCode.Substring(0, 1);
                    shopNew.ShopName = startChar.ToUpper() + txtShopName.Text.Trim();
                }

                shopNew.PhoneNum = txtTelePhone.Text.Trim() != "" ? txtTelePhone.Text.Trim() : "0";
                shopNew.ContactPerson = txtkeeper.Text.Trim() != null ? txtkeeper.Text.Trim() : "0";
                //shopNew.PaperFaceID = cmbPaperFace.SelectedItem!=null?((PaperFace_Info)(cmbPaperFace.SelectedItem)).PaperFaceID:0;
                // shopNew.PaperTypeID = cmbPaperType.SelectedItem!=null?((PaperType_Info)(cmbPaperType.SelectedItem)).PaperTypeID:0;
                shopNew.Priority = Convert.ToInt32(cmbPriority.Text.Trim() != "" ? cmbPriority.Text.Trim() : "0");
                PaperFace_Info selPaperFace = AllPaperFace.FirstOrDefault(p => p.Name == cmbPaperFace.Text.Trim());
                shopNew.PaperFaceID = selPaperFace != null ? selPaperFace.PaperFaceID : 0;

                PaperType_Info selPaperType = AllPaperType.FirstOrDefault(p => p.Name == cmbPaperType.Text.Trim());
                shopNew.PaperTypeID = selPaperType != null ? selPaperType.PaperTypeID : 0;
                shopNew.IsPrintPrice = chkIsNeedPrintUPrice.Checked;
                shopNew.IsAssignByCustom = checkEditIsAssignByCustom.Checked;
                int intVal = _bll.AddNewShop(shopNew);
                if (intVal == 0)
                {
                    ShowSureMsg("商户基础信息添加成功");

                    btnAddNewShop.Enabled = true;
                    simpleButtonSave.Enabled = false;
                    btnUpdateShop.Enabled = true;
                    btnDeleteShop.Enabled = true;
                    intShopBaseOpeType = -1;
                    List<Shop_New_Info> listShop = _bll.GetAllShopList();
                    gridControlShop.DataSource = listShop;
                    if (gridViewShop.RowCount > 1)
                    {
                        gridViewShop.FocusedRowHandle = gridViewShop.RowCount - 1;
                    }
          
                    
                    groupBoxShopBaseInfo.Enabled = false;
                }
                else
                {
                    ShowSureMsg("商户名称重复");
                }
                #endregion

            }
            else if (intShopBaseOpeType == 1)
            {
                #region //修改商户信息
                if (txtShopName.Text.Trim() == "")
                {
                    ShowAlarmMsg("商户名称不能为空！");
                    return;
                }
                if (txtMnemonicCode.Text.Trim() == "")
                {
                    txtMnemonicCode.Text = GetMcode(txtShopName.Text.Trim());

                }
                Shop_New_Info shopUpd = CurrentShopInfo;
                shopUpd.Addr = txtAddress.Text.Trim() != "" ? txtAddress.Text.Trim() : "0";
                shopUpd.AreaName = TxtArea.Text.Trim() != "" ? TxtArea.Text.Trim() : "0";
                shopUpd.CreditLevel = Convert.ToInt32(txtCreditLevel.Text.Trim() == "" ? "0" : txtCreditLevel.Text.Trim());
                shopUpd.Email = txtEmail.Text.Trim() != "" ? txtEmail.Text.Trim() : "0";
                shopUpd.MiniCode = txtMnemonicCode.Text.Trim() != "" ? txtMnemonicCode.Text.Trim() : "0";
                shopUpd.MobilePhoneNum = txtMobile.Text.Trim() != "" ? txtMobile.Text.Trim() : "0";
                shopUpd.MSN = txtMSN.Text.Trim() != "" ? txtMSN.Text.Trim() : "0";
                shopUpd.QQ = txtQQ.Text.Trim() != "" ? txtQQ.Text.Trim() : "0";
                shopUpd.Remark = txtRemark.Text.Trim() != "" ? txtRemark.Text.Trim() : "0";
                shopUpd.SalesMan = txtSalesman.Text.Trim() != "" ? txtSalesman.Text.Trim() : "0";
                shopUpd.ShopID = CurrentShopInfo.ShopID;
                if (shopUpd.MiniCode.Length > 1)
                {
                    String startChar = shopUpd.MiniCode.Substring(0, 1);
                    shopUpd.ShopName = startChar.ToUpper() + txtShopName.Text.Trim();
                }

                shopUpd.PhoneNum = txtTelePhone.Text.Trim() != "" ? txtTelePhone.Text.Trim() : "0";
                shopUpd.ContactPerson = txtkeeper.Text.Trim() != null ? txtkeeper.Text.Trim() : "0";
                //shopNew.PaperFaceID = cmbPaperFace.SelectedItem!=null?((PaperFace_Info)(cmbPaperFace.SelectedItem)).PaperFaceID:0;
                // shopNew.PaperTypeID = cmbPaperType.SelectedItem!=null?((PaperType_Info)(cmbPaperType.SelectedItem)).PaperTypeID:0;
                shopUpd.Priority = Convert.ToInt32(cmbPriority.Text.Trim() != "" ? cmbPriority.Text.Trim() : "0");
                PaperFace_Info selPaperFace = AllPaperFace.FirstOrDefault(p => p.Name == cmbPaperFace.Text.Trim());
                shopUpd.PaperFaceID = selPaperFace != null ? selPaperFace.PaperFaceID : 0;

                PaperType_Info selPaperType = AllPaperType.FirstOrDefault(p => p.Name == cmbPaperType.Text.Trim());
                shopUpd.PaperTypeID = selPaperType != null ? selPaperType.PaperTypeID : 0;
                shopUpd.IsPrintPrice = chkIsNeedPrintUPrice.Checked;
                shopUpd.IsAssignByCustom = checkEditIsAssignByCustom.Checked;
                int intVal = _bll.UpdateShop(shopUpd);
                if (intVal == 0)
                {
                    ShowSureMsg("商户基础信息修改成功");

                    btnAddNewShop.Enabled = true;
                    simpleButtonSave.Enabled = false;
                    btnUpdateShop.Enabled = true;
                    btnDeleteShop.Enabled = true;
                    intShopBaseOpeType = -1;
                    List<Shop_New_Info> listShop = _bll.GetAllShopList();
                    gridControlShop.DataSource = listShop;
                    gridViewShop.FocusedRowHandle = rowhandle;
                    groupBoxShopBaseInfo.Enabled = false;
                }
                else if (intVal == -1)
                {
                    ShowSureMsg("修改商户不存在请重新选择");
                }
                else
                {
                    ShowSureMsg("商户名称已经存在");
                }
                #endregion

            }

        }

        public void ShowSureMsg(string inStr)
        {
            MessageBox.Show(inStr, "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void ShowAlarmMsg(string inStr)
        {
            MessageBox.Show(inStr, "警告", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }






        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAddNewShop.Enabled = true;
            simpleButtonSave.Enabled = false;
            btnUpdateShop.Enabled = true;
            btnDeleteShop.Enabled = true;
            intShopBaseOpeType = -1;
            groupBoxShopBaseInfo.Enabled = false;
        }

        private void btnUpdateShop_Click(object sender, EventArgs e)
        {
            groupBoxShopBaseInfo.Enabled = true;
            btnAddNewShop.Enabled = false;
            simpleButtonSave.Enabled = true;
            btnUpdateShop.Enabled = false;
            btnDeleteShop.Enabled = false;

            intShopBaseOpeType = 1;//修改

            CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();

        }

        private void btnDeleteShop_Click(object sender, EventArgs e)
        {
            CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();
            if (MessageBox.Show("确定要删除" + CurrentShopInfo.ShopName, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
            {
                int intVal = _bll.DelShop(CurrentShopInfo.ShopID);
                if (intVal == 0)
                {
                    ShowSureMsg(CurrentShopInfo.ShopName + "  删除成功");
                }
                else
                {
                    ShowAlarmMsg("删除的商户不存在");
                }
            }
            List<Shop_New_Info> listShop = _bll.GetAllShopList();
            gridControlShop.DataSource = listShop;
            groupBoxShopBaseInfo.Enabled = false;
        }



        private void simpleButtonShopPaperTypePriceUpdate_Click(object sender, EventArgs e)
        {
            //gridControlShopPaperTypePrice.Enabled = true;
            gridColumnUnitPrice.OptionsColumn.AllowEdit = true;
            gcShopPaperFlag.OptionsColumn.AllowEdit = true;
            //gcShopPaperFlag.OptionsColumn.AllowEdit = true;
            simpleButtonShopPaperTypePriceUpdate.Enabled = false;
            simpleButtonSPTPok.Enabled = true;

        }

        private void gridViewShopPaperTypePrice_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.Name == "gridColumnUnitPrice")
                {
                    Shop_Paper_Price_Info shopPaperPriceInfo =
                        (Shop_Paper_Price_Info)gridViewShopPaperTypePrice.GetFocusedRow();
                    shopPaperPriceInfo.isShopPaperPrice = true;
                    //gridControlShopPaperTypePrice.RefreshDataSource();
                }
            }
        }

        private void simpleButtonSPTPok_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrentShopInfo!=null&& currShopPaperPriceInfo != null && currShopPaperPriceInfo.Count > 0)
                {
                    this.Cursor = Cursors.WaitCursor;
                    int intVal = _bll.UpdateShopPaperType_Price(currShopPaperPriceInfo, CurrentShopInfo.ShopID);
                    if (intVal == 0)
                    {
                        ShowSureMsg("纸型单价修改成功！");
                        //gridControlShopPaperTypePrice.Enabled = false;
                        gridColumnUnitPrice.OptionsColumn.AllowEdit = false;
                        gcShopPaperFlag.OptionsColumn.AllowEdit = false;
                        simpleButtonShopPaperTypePriceUpdate.Enabled = true;
                        simpleButtonSPTPok.Enabled = false;
                        //currShopPaperPriceInfo = new List<Shop_Paper_Price_Info>();
                        currShopPaperPriceInfo = _bll.GetPaperPriceByShopId(CurrentShopInfo.ShopID);
                    }
                    else
                    {
                        ShowSureMsg("纸型单价修改失败！");
                    }
                }
            }
            catch (Exception exx)
            {
                ShowAlarmMsg(exx.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            //gridControlShopPaperTypePrice.Enabled = false;
            gridColumnUnitPrice.OptionsColumn.AllowEdit = false;
            gcShopPaperFlag.OptionsColumn.AllowEdit = false;
            simpleButtonShopPaperTypePriceUpdate.Enabled = true;
            simpleButtonSPTPok.Enabled = false;
            currShopPaperPriceInfo = _bll.GetPaperPriceByShopId(CurrentShopInfo.ShopID);
            gridControlShopPaperTypePrice.DataSource = currShopPaperPriceInfo;
            gridControlShopPaperTypePrice.RefreshDataSource();
        }

        private void Tabcontrol1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            //if (Tabcontrol1.SelectedTabPageIndex == 1)
            //{
            //    if(CurrentShopInfo!=null)
            //    {

            //    }
            //}
        }

        private void btnSccipUpdate_Click(object sender, EventArgs e)
        {
            //gridControlShopCommonCostType.Enabled = true;
            {//商户公共收费项目
                gridColumnItemName.OptionsColumn.AllowEdit = true;
                gridColumnSccSinglePrice.OptionsColumn.AllowEdit = true;
                gridColumnSCCTAreaPrice.OptionsColumn.AllowEdit = true;
                gridColumnSCCUnitPrice.OptionsColumn.AllowEdit = true;
                gridColumnSCCTypeName.OptionsColumn.AllowEdit = true;
                GCCommFlag.OptionsColumn.AllowEdit = true;
            }
            btnSccipSave.Enabled = true;
            btnSccipUpdate.Enabled = false;


        }

        private void btnsccipCancel_Click(object sender, EventArgs e)
        {
            // gridControlShopCommonCostType.Enabled = false;
            {//商户公共收费项目
                gridColumnItemName.OptionsColumn.AllowEdit = false;
                gridColumnSccSinglePrice.OptionsColumn.AllowEdit = false;
                gridColumnSCCTAreaPrice.OptionsColumn.AllowEdit = false;
                gridColumnSCCUnitPrice.OptionsColumn.AllowEdit = false;
                gridColumnSCCTypeName.OptionsColumn.AllowEdit = false;
                GCCommFlag.OptionsColumn.AllowEdit = false;
            }
            btnSccipSave.Enabled = false;
            btnSccipUpdate.Enabled = true;
            ListShopCommonChangeItemPriceInfo =
                       _bll.GetShopCommonChangeItemPriceInfoByShopId(CurrentShopInfo.ShopID);

            gridControlShopCommonCostType.DataSource = ListShopCommonChangeItemPriceInfo;
            gridControlShopCommonCostType.RefreshDataSource();

        }

        private void btnSccipSave_Click(object sender, EventArgs e)
        {

            try
            {
                if (currShopPaperPriceInfo != null && ListShopCommonChangeItemPriceInfo.Count > 0)
                {
                    if (CurrentShopInfo.ShopID == ListShopCommonChangeItemPriceInfo[0].Shop.ShopID)
                    {
                        this.Cursor = Cursors.WaitCursor;
                        int intVal = _bll.UpdateShopCommonChangeItemPriceInfo(ListShopCommonChangeItemPriceInfo,CurrentShopInfo.ShopID);
                        if (intVal == 0)
                        {
                            ShowSureMsg("基本价格更新成功");
                            {//商户公共收费项目
                                gridColumnItemName.OptionsColumn.AllowEdit = false;
                                gridColumnSccSinglePrice.OptionsColumn.AllowEdit = false;
                                gridColumnSCCTAreaPrice.OptionsColumn.AllowEdit = false;
                                gridColumnSCCUnitPrice.OptionsColumn.AllowEdit = false;
                                gridColumnSCCTypeName.OptionsColumn.AllowEdit = false;
                                GCCommFlag.OptionsColumn.AllowEdit = false;
                            }
                            btnSccipSave.Enabled = false;
                            btnSccipUpdate.Enabled = true;

                        }
                        else
                        {
                            ShowAlarmMsg("更新失败");
                        }
                    }
                    else
                    {
                        ShowSureMsg("请重新选择商户");

                    }
                }
                else
                {
                    ShowSureMsg("请选择商户");
                }
            }
            catch (Exception ex)
            {
                ShowAlarmMsg(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnShopProductPriceUpdate_Click(object sender, EventArgs e)
        {
            btnShopProductPriceUpdate.Enabled = false;
            //btnShopProductPriceUpdate.
            //gridControlProduct.Enabled = true;
            {
                IsChecked.OptionsColumn.AllowEdit = true;
                gridColumn23.OptionsColumn.AllowEdit = true;
                gridColumn2.OptionsColumn.AllowEdit = true;
                gridColumn3.OptionsColumn.AllowEdit = true;
                gridColumn4.OptionsColumn.AllowEdit = true;

            }
            btnShopProductPriceOK.Enabled = true;
            simpleButtonSelectAll.Enabled = true;
            simpleButtonSelectOther.Enabled = true;
        }

        private void gridViewProduct_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                ProductDDO ddo = (ProductDDO)gridViewProduct.GetRow(e.RowHandle);
                if (e.Column.Name == "IsChecked")
                {

                }
                else
                {
                    ddo.IsShopProduct = true;


                }
                ddo.Status = 1;


            }
        }

        private void btnShopProductPriceOK_Click(object sender, EventArgs e)
        {
            try
            {


                if (CurrentShopInfo.ShopID == ListProduct[0].ShopId)
                {
                    if (MessageBox.Show("确认更改商户" + CurrentShopInfo.ShopName + "产品信息", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                    {
                        this.Cursor = Cursors.WaitCursor;
                        List<ProductDDO> productOut = _bll.UpdateShopProductPrice(ListProduct, CurrentShopInfo.ShopID);
                        if (productOut == null || productOut.Count == 0)
                        {
                            ShowSureMsg("商户 " + CurrentShopInfo.ShopName + " 产品信息修改成功");
                            CurrentShopInfo = (Shop_New_Info)gridViewShop.GetFocusedRow();
                            ListProduct = _bll.GetAllShopProducts(CurrentShopInfo.ShopID);
                            gridControlProduct.DataSource = ListProduct;
                        }
                        else
                        {
                            string productids = "";
                            foreach (ProductDDO ddo in productOut)
                            {
                                productids += ddo.ProuctId + ",";
                            }
                            ShowSureMsg("产品 " + productids + " 产品信息修改失败,请检查");

                        }
                    }
                }
            }
            catch (Exception exception)
            {

                ShowAlarmMsg(exception.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            btnShopProductPriceUpdate.Enabled = true;
            // gridControlProduct.Enabled = false;
            {//商户价格
                IsChecked.OptionsColumn.AllowEdit = false;
                gridColumn23.OptionsColumn.AllowEdit = false;
                gridColumn2.OptionsColumn.AllowEdit = false;
                gridColumn3.OptionsColumn.AllowEdit = false;
                gridColumn4.OptionsColumn.AllowEdit = false;
            }
            btnShopProductPriceOK.Enabled = false;
            simpleButtonSelectAll.Enabled = false;
            simpleButtonSelectOther.Enabled = false;
        }

        private void btnShopProductPriceCancel_Click(object sender, EventArgs e)
        {
            btnShopProductPriceUpdate.Enabled = true;
            //gridControlProduct.Enabled = false;
            {
                IsChecked.OptionsColumn.AllowEdit = false;
                gridColumn23.OptionsColumn.AllowEdit = false;
                gridColumn2.OptionsColumn.AllowEdit = false;
                gridColumn3.OptionsColumn.AllowEdit = false;
                gridColumn4.OptionsColumn.AllowEdit = false;
            }
            btnShopProductPriceOK.Enabled = false;
            simpleButtonSelectAll.Enabled = false;
            simpleButtonSelectOther.Enabled = false;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            // obj.shop_DistributionInfo = distributionInfo;
            groupBoxShopDistrion.Enabled = true;
            simpleButton2.Enabled = true;
            simpleButton1.Enabled = false;
        }

        public void DisPlayDisribution(shop_DistributionInfo_Info indistributionInfo)
        {
            txtBusTimetable.Text = indistributionInfo.BusTimetable;
            txtConsigneeName.Text = indistributionInfo.ConsigneeName;
            txtConsigneeTelePhone.Text = indistributionInfo.ConsigneeTelePhone;
            txtDistributionAddress.Text = indistributionInfo.DistributionAddress;
            txtDistributionMode.Text = indistributionInfo.DistributionMode;
            txtFarePayer.Text = indistributionInfo.FarePayer;
            txtdisributionReMark.Text = indistributionInfo.ReMark;
        }

        private void gridControlShop_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton8_Click(object sender, EventArgs e)
        {
            groupBoxShopDistrion.Enabled = false;
            simpleButton2.Enabled = false;
            simpleButton1.Enabled = true;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {


            try
            {

                shopDistributionInfo.BusTimetable = txtBusTimetable.Text;
                shopDistributionInfo.ConsigneeName = txtConsigneeName.Text;
                shopDistributionInfo.ConsigneeTelePhone = txtConsigneeTelePhone.Text;
                shopDistributionInfo.DistributionAddress = txtDistributionAddress.Text;
                shopDistributionInfo.DistributionMode = txtDistributionMode.Text;
                shopDistributionInfo.FarePayer = txtFarePayer.Text;
                shopDistributionInfo.ReMark = txtdisributionReMark.Text;
                this.Cursor = Cursors.WaitCursor;
                int intVal = _bll.UpdateShopDistributionInfo(shopDistributionInfo, CurrentShopInfo.ShopID);
                if (intVal == 0)
                {
                    ShowSureMsg("商户" + CurrentShopInfo.ShopName + " 货运信息修该成功");
                }
                groupBoxShopDistrion.Enabled = false;
                simpleButton2.Enabled = false;
                simpleButton1.Enabled = true;

            }
            catch (Exception ex)
            {
                ShowAlarmMsg(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void FrmShopManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

        private void simpleButtonSelectAll_Click(object sender, EventArgs e)
        {
            if(this.CurrentShopInfo!=null&&this.ListProduct!=null&&ListProduct.Count>0)
            {
                ListProduct.Where(p=>p.IsShopProduct2==false).ToList().ForEach(p=>p.IsShopProduct=true);
                ListProduct.Where(p=>p.IsShopProduct2==false).ToList().ForEach(p => p.Status = 1);
                gridControlProduct.RefreshDataSource();
            }
            else
            {
                ShowSureMsg("请选择商户");
            }
        }

        private void simpleButtonSelectOther_Click(object sender, EventArgs e)
        {
            if (this.CurrentShopInfo != null && this.ListProduct != null && ListProduct.Count > 0)
            {
                ListProduct.Where(p=>p.IsShopProduct2==true).ToList().ForEach(p => p.IsShopProduct = false);
                ListProduct.Where(p=>p.IsShopProduct2==true).ToList().ForEach(p => p.Status = 1);
                gridControlProduct.RefreshDataSource();
            }
            else
            {
                ShowSureMsg("请选择商户");
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            ////saveFileDialog.AddExtension = true;
            //saveFileDialog.FilterIndex = 3;
            //saveFileDialog.RestoreDirectory = true;
            //if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
               // string fileName = saveFileDialog.FileName;
               
            //    if (saveFileDialog.OpenFile() != null)
            //    {
            //        switch (saveFileDialog.FilterIndex)
            //        {
            //            case 3:
            //                gridControlShop.ExportToXls(fileName);
            //                return;
            //            case 4:
            //                gridControlShop.ExportToHtml(fileName);
            //                return;
            //            case 2:
            //                gridControlShop.ExportToPdf(fileName);
            //                return;
            //            case 1:
            //                this.gridControlShop.ExportToText(fileName);
            //                return;
            //            default:
            //                break;
            //        }
            //    }

            //}
            gridControlShop.ExportToExcel(this, "商户基本信息");
        }

        private void simpleButtonModefyProductPrice_Click(object sender, EventArgs e)
        {
            ProductDDO ddo = (ProductDDO)gridViewProduct.GetFocusedRow();
            if (ddo!=null)
            {
                FrmModifyProductPrice frm = new FrmModifyProductPrice(ddo);
                frm.ShowDialog();
            }
            else
            {
                ShowSureMsg("请选择一个产品");
            }

        }

        private void simpleButtonPaperTypeApply_Click(object sender, EventArgs e)
        {
            Shop_Paper_Price_Info sppi = (Shop_Paper_Price_Info)gridViewShopPaperTypePrice.GetFocusedRow();
            if (sppi != null)
            {
                FrmModifyProductPrice frm = new FrmModifyProductPrice(sppi);
                frm.ShowDialog();
            }
            else
            {
                ShowSureMsg("请选择一个纸型");
            }
        }

        private void simpleButtonAppComm_Click(object sender, EventArgs e)
        {
            Shop_CommonChargeItem_Price_Info info = (Shop_CommonChargeItem_Price_Info)gridViewShopCommonCostType.GetFocusedRow();
            if (info != null)
            {
                FrmModifyProductPrice frm = new FrmModifyProductPrice(info);
                frm.ShowDialog();
            }
            else
            {
                ShowSureMsg("请选择一个公共收费项目");
            }
        }

        private void simpleButtonShopExport_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            ////saveFileDialog.AddExtension = true;
            //saveFileDialog.FilterIndex = 3;
            //saveFileDialog.RestoreDirectory = true;
            //if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    string fileName = saveFileDialog.FileName;
            //    if (saveFileDialog.OpenFile() != null)
            //    {
            //        switch (saveFileDialog.FilterIndex)
            //        {
            //            case 3:
            //                gridControlProduct.ExportToXls(fileName);
            //                return;
            //            case 4:
            //                gridControlProduct.ExportToHtml(fileName);
            //                return;
            //            case 2:
            //                gridControlProduct.ExportToPdf(fileName);
            //                return;
            //            case 1:
            //                this.gridControlProduct.ExportToText(fileName);
            //                return;
            //            default:
            //                break;
            //        }
            //    }

            //}
            gridControlProduct.ExportToExcel(this, "产品信息");
        }

        private void checkBoxDisplayDelShop_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxDisplayDelShop.CheckState == CheckState.Checked)
            {
                this.gridControlShop.DataSource = _bll.GetAllShopList();
            }
            else
            {
                List<Shop_New_Info> list=_bll.GetAllShopList().Where(st => st.Status == 1).ToList();
                this.gridControlShop.DataSource = list;
            }
            gridControlShop.RefreshDataSource();
        }

        private void btnFtpEnable_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("是否要重置商户 " + CurrentShopInfo.ShopName + " 的认证码，重置后商户必须重新认证才能下单", "确认", MessageBoxButtons.OK, MessageBoxIcon.Question))
            {
                string newShopCode = _bll.ResetShopShaCode(CurrentShopInfo.ShopID);
                if (!string.IsNullOrEmpty(newShopCode))
                {
                    textEditShaCode.Text = newShopCode;

                }
                else
                {
                    MessageBox.Show("重置未成功，请稍候再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

 
            }
        }

        private void gridViewShop_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                rowhandle = e.RowHandle;
                if (Tabcontrol1.SelectedTabPageIndex == 0)//详细信息
                {
                    #region//基础信息
                    if (intShopBaseOpeType != 1)//不是更新可变
                    {
                        Shop_New_Info curShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.RowHandle);
                        txtAddress.Text = curShopInfo.Addr;
                        TxtArea.Text = curShopInfo.AreaName;
                        txtCreditLevel.Text = curShopInfo.CreditLevel.ToString();
                        txtEmail.Text = curShopInfo.Email;
                        txtMnemonicCode.Text = curShopInfo.MiniCode;
                        txtMobile.Text = curShopInfo.MobilePhoneNum;
                        txtMSN.Text = curShopInfo.MSN;
                        txtQQ.Text = curShopInfo.QQ;
                        txtRemark.Text = curShopInfo.Remark;
                        txtSalesman.Text = curShopInfo.SalesMan;
                        txtShopName.Text = curShopInfo.ShopName.Substring(1);
                        txtTelePhone.Text = curShopInfo.PhoneNum;
                        cmbPaperFace.Text = AllPaperFace.FirstOrDefault(p => p.PaperFaceID == curShopInfo.PaperFaceID) != null ?
                            AllPaperFace.FirstOrDefault(p => p.PaperFaceID == curShopInfo.PaperFaceID).Name : "";
                        cmbPaperType.Text = AllPaperType.FirstOrDefault(P => P.PaperTypeID == curShopInfo.PaperTypeID) != null ?
                            AllPaperType.FirstOrDefault(P => P.PaperTypeID == curShopInfo.PaperTypeID).Name : "";
                        cmbPriority.Text = curShopInfo.Priority.ToString();
                        chkIsNeedPrintUPrice.Checked = curShopInfo.IsPrintPrice;
                        txtkeeper.Text = curShopInfo.ContactPerson;
                        checkEditIsAssignByCustom.Checked = curShopInfo.IsAssignByCustom;
                    }
                    #endregion

                }
                else if (Tabcontrol1.SelectedTabPageIndex == 1)//计价收费
                {
                    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.RowHandle);
                    currShopPaperPriceInfo = _bll.GetPaperPriceByShopId(CurrentShopInfo.ShopID);
                    gridControlShopPaperTypePrice.DataSource = currShopPaperPriceInfo;

                    ListShopCommonChangeItemPriceInfo =
                        _bll.GetShopCommonChangeItemPriceInfoByShopId(CurrentShopInfo.ShopID);

                    gridControlShopCommonCostType.DataSource = ListShopCommonChangeItemPriceInfo;

                }
                else if (Tabcontrol1.SelectedTabPageIndex == 2)//产品列表
                {
                  
                    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.RowHandle);
                   
                    ListProduct = _bll.GetAllShopProducts(CurrentShopInfo.ShopID);
                    gridControlProduct.DataSource = ListProduct;

                }
                else if (Tabcontrol1.SelectedTabPageIndex == 3)//货运信息
                {
                    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.RowHandle);
                    shopDistributionInfo = _bll.GetShopDistributionInfo(CurrentShopInfo.ShopID);
                    DisPlayDisribution(shopDistributionInfo);
                }
                else//远程FTP
                {
                    CurrentShopInfo = (Shop_New_Info)gridViewShop.GetRow(e.RowHandle);
                    textEditShopName.Text = CurrentShopInfo.ShopName;
                    txtFtpLoginId.Text = CurrentShopInfo.FtpLoginName;
                    txtftpPwd.Text = CurrentShopInfo.FtpLoginPWD;
                    textEditShaCode.Text = CurrentShopInfo.SHACODE;
                    checkBoxPower.Checked = CurrentShopInfo.IsForceCheck;
                }
            }
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("是否要重置商户 " + CurrentShopInfo.ShopName + ",重置后需要重新注册", "确认", MessageBoxButtons.OK, MessageBoxIcon.Question))
            {
                string newShopCode = _bll.ResetShopPwd(CurrentShopInfo.ShopID);
                if (!string.IsNullOrEmpty(newShopCode))
                {
                    txtFtpLoginId.Text = ""; ;
                    txtftpPwd.Text = "";
                }
                else
                {
                    MessageBox.Show("重置未成功，请稍候再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }


            }
        }

        private void simpleButtonsetCheck_Click(object sender, EventArgs e)
        {
            if (CurrentShopInfo != null)
            {
                string msg = checkBoxPower.Checked ? "强制审核" : "不强制审核";
                if (MessageBox.Show("是否" + msg + CurrentShopInfo.ShopName+"的远程开单", "询问", MessageBoxButtons.OK, MessageBoxIcon.Question) == DialogResult.OK)
                {
                   int rtnVal= _bll.SetForceCheck(CurrentShopInfo.ShopID, checkBoxPower.Checked);
                   if (rtnVal == 1)
                   {
                       MessageBox.Show("设置成功" + CurrentShopInfo.ShopName + "已经改为" + msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                   }
                   else
                   {
                       MessageBox.Show("设置失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
                   }
 
                }
            }

        }

        private void txtMnonicCodeSearchShop_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            List<Shop_New_Info> listShop =null;
            if (e.NewValue.ToString() != string.Empty)
            {
                listShop = _bll.GetShopListByMnemonicCode(e.NewValue.ToString());
                
            }
            else
            {
                listShop = _bll.GetAllShopList();
            }
            gridControlShop.DataSource = listShop;
            gridControlShop.RefreshDataSource();
        }

        private void txtMnonicCodeSearchShop_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void gridControlProduct_Click(object sender, EventArgs e)
        {

        }

        private void simpleButtonAllPriceCopy_Click(object sender, EventArgs e)
        {
            if (CurrentShopInfo == null)
            {
                MessageBox.Show("提示","请选择商户",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            if (ListProduct == null || ListProduct.Count == 0)
            {
                MessageBox.Show("提示", "请先为商户<" + CurrentShopInfo.ShopName+">定价", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            FrmModifyAllProductPrice frm = new FrmModifyAllProductPrice(CurrentShopInfo);
            frm.ShowDialog();
        }

       

   
       



    }
}
